package edu.mtu.citizenscience.cs.sdk.uploader.upload;

import android.util.Log;

import edu.mtu.citizenscience.cs.sdk.ResponseHandler;
import edu.mtu.citizenscience.cs.sdk.json.Response;
import edu.mtu.citizenscience.cs.sdk.uploader.CitsciUploaderError;
import edu.mtu.citizenscience.cs.sdk.uploader.CitsciUploaderService;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Abstract class from which the holds shared code bettween the diffrent parts of the upload
 */
public abstract class UploadPart {
	// upload service that is executing the upload
	protected CitsciUploaderService uploader;
	
	/**
	 * Create upload part
	 * 
	 * @param   citsciUploaderService - CitsciUploaderService - the uploader service
	 * @return	void
	 */
	public UploadPart(CitsciUploaderService citsciUploaderService)
	{
		this.uploader = citsciUploaderService;
	}
	
	/**
	 * @since 1.0
	 * 
	 * Abstract response handler for each upload part
	 */
	public abstract class UploadResponseHandler<E> extends ResponseHandler<E> {

		/**
		 * Create upload response handler
		 * 
		 * @param   playloadType - Class<?> - the response payload type
		 * @return	void
		 */
		public UploadResponseHandler(Class<?> playloadType) 
		{
			super(playloadType);
		}

		/**
		 * Called on a valid response from the server
		 * 
		 * @param   response - Response<E> - The response from the server
		 * @return	void
		 */
		public abstract void response(Response<E> response);

		/**
		 * Called on a error from the server
		 * 
		 * @param   response - Response<E> - The response from the server
		 * @return	void
		 */
		@Override
		public void error(Response<E> response) 
		{
			// print out the developer error message
			Log.e("Uploader", response.getError().getDeveloperMessage());
			// pass the user error message onto the activity
			uploader.sendErrorMessage(response.getError().getUserMessage());
		}

		/**
		 * Called on an exception in parsing the response from the server into JSON
		 * 
		 * @param   void
		 * @return	void
		 */
		@Override
		public void exception(Exception exception) 
		{
			// pass onto the uploader that got an invalid response
			uploader.sendError(CitsciUploaderError.INVALID_RESPONSE);
			
			// handle the exception in the basic response handler
			super.exception(exception);
		}

		/**
		 * Called on no response from the server
		 * 
		 * @param   void
		 * @return	void
		 */
		@Override
		public void noResponse() 
		{
			// pass the no connection error onto the uploader
			uploader.sendError(CitsciUploaderError.NO_CONNECTION);
		}
		
	}
}
